import {defineStore} from "pinia";
import {loginApi} from "../api/userApi.js";
import {removeToken, setToken} from "../utils/token-tuils.js";

/**
 * 用户信息
 */
export const defineUser = defineStore('safeUser', {
    state: () => {
        return {
            token: '',
            uid: 0,
            username: '',
            account: '',
            avatarUrl: '',
            age: 0,
            gender: 0
        }
    },
    actions: {
        //登录的异步action
        async login(loginForm) {
            // 发送登录的请求
            const {data} = await loginApi(loginForm)
            //请求成功后将token保存
            const token = data.data.token
            this.token = token
            setToken(token)
            return data
        }
        ,
        //清除用户信息
        initUserInfo() {
            removeToken()
            this.token = ''
            this.uid = -1
            this.username = ''
            this.account = ''
            this.avatarUrl = ''
            this.age = -1
            this.gender = -1
        }
    }
})
